package study;

import time.Timewait;

/**
 * StringBuffer的用法和StringBuilder一样
 * 两者的区别：
 *      StringBuilder: 非线程安全的，并发处理的，性能稍快
 *      StringBuffer:  线程安全的，同步处理的，性能稍慢
 * 一般使用StringBuilder
 * */
public class StringBuilderDemo {
    public static void main(String[] args) {
        int n = 100000;
        String time = Timewait.timewait(() -> {
            String str = "";
            for (int i = 0; i < n; i++)
                str += i;
        });
        System.out.println(time);
        time = Timewait.timewait(() -> {
            StringBuilder strB = new StringBuilder();
            for (int i = 0; i < n; i++)
                strB.append(i);
        });
        System.out.println(time);

        StringBuilder strB = new StringBuilder("cgk");
        strB.append("ff");
        String str = strB.toString();
        System.out.println(str);
        strB.delete(3, 4);
        System.out.println(strB);
        strB.replace(2, 3, "gqe3");
        System.out.println(strB);
        strB.insert(0, "在指定位置插入字符串");
        System.out.println(strB);
    }
}
